var bodyParser = require('body-parser') // 提交表单，将数据格式化
var jsonParser = bodyParser.json() // create application/json parser 
var urlencodedParser = bodyParser.urlencoded({ extended: true })// create application/x-www-form-urlencoded parser 

var Index = require('../app/controllers/index')
var User = require('../app/controllers/user')
var Movie = require('../app/controllers/movie')

module.exports = function (app) {

	// pre handle user
	app.use(function (req, res, next) {
		var _user = req.session.user
		app.locals.user = _user
		return next()
	})


	app.get('/', Index.index)

	// signup
	app.get('/user/list', User.signinRequired, User.adminRequired, User.list)
	app.post('/user/signup', urlencodedParser, User.signup)
	app.post('/user/signin', urlencodedParser, User.signin)
	app.get('/logout', User.logout)
	app.get('/signin', User.showSignin)
	app.get('/signup', User.showSignup)

	// admin page
	app.get('/movie/:id', Movie.detail)
	app.get('/admin/movie', User.signinRequired, User.adminRequired, Movie.add)
	app.get('/admin/movie/update/:id', User.signinRequired, User.adminRequired, Movie.update)
	app.post('/admin/movie/new', urlencodedParser, User.signinRequired, User.adminRequired, Movie.save)
	app.get('/admin/movie/list', User.signinRequired, User.adminRequired, Movie.list)
	app.delete('/admin/movie/list', urlencodedParser, User.signinRequired, User.adminRequired, Movie.del)


}